namespace MyCompanyName.AbpZeroTemplate.Migrations
{
    using Abp.Events.Bus;
    using Abp.Events.Bus.Entities;
    using Abp.MultiTenancy;
    using Abp.Zero.EntityFramework;
    using global::EntityFramework.DynamicFilters;
    using MyCompanyName.AbpZeroTemplate.Migrations.Seed.Host;
    using MyCompanyName.AbpZeroTemplate.Migrations.Seed.Tenants;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    public sealed class Configuration : DbMigrationsConfiguration<MyCompanyName.AbpZeroTemplate.EntityFramework.AbpZeroTemplateDbContext>, IMultiTenantSeed
    {
        public AbpTenantBase Tenant { get; set; }
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "AbpZeroTemplate";
            
            //SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
        }

        protected override void Seed(MyCompanyName.AbpZeroTemplate.EntityFramework.AbpZeroTemplateDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //


            context.DisableAllFilters();

            context.EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
            context.EventBus = NullEventBus.Instance;

            if (Tenant == null)
            {
                //Host seed
                new InitialHostDbBuilder(context).Create();

                //Default tenant seed (in host database).
                new DefaultTenantBuilder(context).Create();
                new TenantRoleAndUserBuilder(context, 1).Create();
            }
            else
            {
                //You can add seed for tenant databases using Tenant property...
            }

            context.SaveChanges();
        }
    }
}
